#!/usr/bin/perl -w
# Create a nested Javascript record set of MCAS subject test scores
# from a tab-delimited input file with 15 fields, including a header row
# at the top of the file:
#    (0) Year, Student (encrypted SASID), Grade, Projected_Graduation_Year, 
#    (4) Subject_ID, Subject, Test_Status, 
#    (7) Performance_Level, Performance_Level_Clean, 
#    (9) Alt_Assessment,
#   (10) Raw_Score, Maximum_Raw_Score, Scaled_Score, CPI, and 
#   (14) School.
#
# MCAS = { subject1: 
#           { grade1: 
#               { year1: 
#                   { [test1, test2...] }, 
#                 year2:
#                    ...
#               }, 
#             grade2:
#               ...
#           }, 
#          subject2:
#           ... 
#        };
# where each 'test' = [ student, test_status, performance_level,
#   performance_level_clean, alt_assessment, raw_score, maximum_raw_score,
#   scaled_score, CPI, school ].

print "var MCAS = {";
my $headers = <>; chomp $headers;
my $prev_subject = 'none';
my $prev_year = 0;
my $prev_grade = 0;
while (<>) {
    chomp;
    (my $year, my $student, my $grade, my $projected_graduation_year,
    my $subject_id, my $subject, my $test_status, my $performance_level,
    my $performance_level_clean, my $alt_assessment, my $raw_score,
    my $maximum_raw_score, my $scaled_score, my $CPI, my $school) = split /\t/;
    if ($subject ne $prev_subject) {
        if ($prev_subject ne 'none') {
            print ']}},';
            $prev_grade = 0;
            $prev_year = 0;
        }
        print "\n", $subject, ':{';
        $prev_subject = $subject;
    }
    if ($grade != $prev_grade) {
        if ($prev_grade) {
            print ']},';
            $prev_year = 0;
        }
        print "\n", $grade, ':{';
        $prev_grade = $grade;
    }
    if ($year != $prev_year) {
        if ($prev_year) {
            print '],';
        }
        print $year, ':[';
        $prev_year = $year;
    }
    $, = ',';
    print '[' . $student, 
        "'" . $test_status . "'", 
        "'" . $performance_level . "'",
        "'" . $performance_level_clean . "'",
        "'" . $alt_assessment . "'",
        $raw_score,
        $maximum_raw_score,
        $scaled_score,
        $CPI,
        "'" . $school . "']," . "\n";
    $, = undef;
}
if ($prev_year) {
    print ']';
}
if ($prev_grade) {
    print '}';
}
if ($prev_subject ne 'none') {
    print '}';
}
print "};\n";
